﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Threading;

namespace Erebos.GameScreenFiles
{
    class GameScreen : Screen
    {
        private Charackter _charackter;
        private World _world;
        private float _elapsed;
        private float _frameRate;
        private float _frames;
        private float _uelapsed;
        private float _uRate;
        private float _updates;

        private float _rotation = 0.0f;
        private InterfaceOverlay _overlay;

        public GameScreen(Charackter charackter)
        {
            _charackter = charackter;
            Thread createWorldThread = new Thread(LoadWorld);
            createWorldThread.Start();
            _overlay = new InterfaceOverlay(ref _charackter);
        }

        private void LoadWorld()
        {
            _world = new World(1, ScreenSizeHelper.Device, MainGame.Effect);
        }

        public override void DrawBefore(GameTime gameTime)
        {
            if (_world == null || _world.IsGenerate)
                return;

            _world.Draw(gameTime);
            base.DrawBefore(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            if (_world == null || _world.IsGenerate)
            {
                Vector2 sizeText = MainGame.Fonts["Arial16"].MeasureString("Bitte warten...");
                Vector2 sizeRad = new Vector2(MainGame.Textures["Zahnrad"].Width / 4, MainGame.Textures["Zahnrad"].Height / 4);
                Vector2 size = new Vector2(sizeText.X + sizeRad.X, sizeRad.Y);

                spriteBatch.Draw(MainGame.Textures["Zahnrad"], new Rectangle(ScreenSizeHelper.SizeWidth / 2 - (int)size.X / 2 - (int)sizeRad.X / 2, MainGame.Textures["Zahnrad"].Height / 8, MainGame.Textures["Zahnrad"].Width / 4, MainGame.Textures["Zahnrad"].Height / 4), new Rectangle(0, 0, MainGame.Textures["Zahnrad"].Width, MainGame.Textures["Zahnrad"].Height), Color.White, _rotation, new Vector2(MainGame.Textures["Zahnrad"].Width / 2, MainGame.Textures["Zahnrad"].Height / 2), SpriteEffects.None, 0);
                spriteBatch.DrawString(MainGame.Fonts["Arial16"], "Bitte warten...", new Vector2(ScreenSizeHelper.SizeWidth / 2 - (int)size.X / 2, MainGame.Textures["Zahnrad"].Height / 8 - (int)(MainGame.Fonts["Arial16"].MeasureString("XX").Y / 2)), Color.White);
                return;
            }

            _elapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (_elapsed > 1.0f)
            {
                _elapsed -= 1.0f;
                _frameRate = _frames;
                _frames = 0;
            }
            else
            {
                _frames += 1;
            }

            spriteBatch.DrawString(MainGame.Fonts["Arial12"], "Camera Position: " + Camera.CameraPosition.ToString(), new Vector2(0, 0), Color.White);
            spriteBatch.DrawString(MainGame.Fonts["Arial12"], "Camera Rotation (LR): " + Camera.LeftRightRot.ToString(), new Vector2(0, 15), Color.White);
            spriteBatch.DrawString(MainGame.Fonts["Arial12"], "Camera Rotation (UD): " + Camera.UpDownRot.ToString(), new Vector2(0, 30), Color.White);
            spriteBatch.DrawString(MainGame.Fonts["Arial12"], "FPS: " + _frameRate.ToString("0"), new Vector2(0, 45), Color.White);
            
            //Overlay
            _overlay.Draw(gameTime, spriteBatch);

            base.Draw(spriteBatch, gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            if (_world == null || _world.IsGenerate)
            {
                _rotation += 0.05f;
                return; 
            }

            _world.Update(gameTime);

            //Overlay
            _overlay.Update(gameTime);

            base.Update(gameTime);
        }
    }
}
